package org.dubnation.base.excel.download.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import org.dubnation.base.excel.download.service.IDownloadService;
import org.dubnation.base.excel.download.util.DownloadNeed;
import org.dubnation.base.excel.download.vo.DepartmentVO;
import org.dubnation.base.excel.download.vo.UploadErrorMsgVO;
import org.dubnation.base.exception.BusinessException;
import org.dubnation.base.exception.FileException;
import org.dubnation.org.domain.Employee;
import org.dubnation.org.mapper.EmployeeMapper;
import org.dubnation.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;


@Service
@Transactional
public class DownloadServiceImpl implements IDownloadService {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 导出文件
     * @param downloadNeed
     */
    @Override
    public void download(DownloadNeed downloadNeed, HttpServletResponse response) {
        //获取数据
        List<DepartmentVO> departmentVOS = departmentService.queryDepInfo(downloadNeed);
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("部门信息", "部门"),
                DepartmentVO.class, departmentVOS);
        FileOutputStream outputStream = null;
        try {
            //写出
            outputStream = new FileOutputStream("D:\\360MoveData\\Users\\admin\\Desktop\\department.xlsx");
            //设置请求头，解决文件名中文乱码的问题
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("department.xlsx", "utf-8"));
            workbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
            throw new FileException(e.getMessage());
        } finally {
            try {
                //关流
                outputStream.close();
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    /**
     * 导出上传的错误信息
     * @param errorMsgList
     */
    @Override
    public void exportErrorMsg(List<UploadErrorMsgVO> errorMsgList) {
        Workbook errorWorkbook = ExcelExportUtil.exportExcel(new ExportParams("错误信息", "错误信息"), UploadErrorMsgVO.class, errorMsgList);
        //获取输出流
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream("D:\\360MoveData\\Users\\admin\\Desktop\\errormsg.xlsx");
            errorWorkbook.write(outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //关流
                outputStream.close();
                errorWorkbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
